﻿<template>
  <a-drawer
    :title="title"
    width="40%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @close="()=>{this.visible=false}"
  >
    <a-spin :spinning="loading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="供应商类型编码" prop="SupplierTypeCode">
          <a-input :disabled="disabled" v-model="entity.SupplierTypeCode" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="供应商类型名称" prop="SupplierTypeName">
          <a-input v-model="entity.SupplierTypeName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="是否启用编码规格" prop="IsEnable">
          <!-- <a-radio-group v-model="entity.IsEnable" @change="IsEnable" :default-value="false" button-style="solid">
            <a-radio-button :value="false"> 否 </a-radio-button>
            <a-radio-button :value="true"> 是 </a-radio-button>
          </a-radio-group> -->
          <a-radio-group
                @change="IsEnable"
                v-model="entity.IsEnable"
                :default-value="false"
                button-style="solid"
              >
                <a-radio-button :value="true"> 是 </a-radio-button>
                <a-radio-button :value="false"> 否 </a-radio-button>
              </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="编码规则" prop="RuleCode" v-if="entity.IsEnable"> 
          <a-input v-model="entity.RuleCode" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="序号" prop="SortIndex">
          <a-input v-model="entity.SortIndex" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="备注" prop="Remarks">
          <a-input v-model="entity.Remarks" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button
        :style="{ marginRight: '8px' }"
        @click="
          () => {
            this.visible = false
          }
        "
        >取消</a-button
      >
      <a-button type="primary" @click="handleSubmit">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    parentObj: Object
  },
  data() {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      loading: false,
      entity: {
        IsEnable: false
      },
      rules: {},
      title: '',
      disabled:false
    }
  },
  methods: {
    IsEnable(e) {
      this.disableds = e.target.value
      this.disableds ? (this.entity.Ratio = 0) : (this.entity.Ratio = '')
    },
    init() {
      this.visible = true
      this.entity = {
        IsEnable: false
      }
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    openForm(id, title) {
      this.init()
      this.title = title
      this.disabled=false
      if (id) {
        this.loading = true
        this.$http.post('/PB/pb_suppliertype/GetTheData', { id: id }).then(resJson => {
          this.loading = false
          this.entity = resJson.Data
        })
        this.disabled=true
      }
    },
    handleSubmit() {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.loading = true
        if(!this.entity.IsEnable){
          this.entity.RuleCode=''
        }
        this.$http.post('/PB/pb_suppliertype/SaveData', this.entity).then(resJson => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
